package controllers

import (
	"encoding/json"
	"github.com/astaxie/beego"
	"youkugo/models"
)

type UserController struct {
	beego.Controller
}

//用户注册功能
// @router /register/save [get]
func (this *UserController) SaveRegister() {
	var (
		mobile   string
	)
	mobile = this.GetString("mobile")
	if mobile == "" {
		this.Data["json"] = ReturnError(4001, "手机号不能为空")
		this.ServeJSON()
	}
}
//用户注册
// @router /user/register [post]
func (this *UserController) Register() {
	var user models.User
	data := this.Ctx.Input.RequestBody
	err := json.Unmarshal(data, &user)
	if err == nil {
		mobile := user.Mobile
		if mobile == "" {
			this.Data["json"] = ReturnError(4001, "手机号不能为空")
		}
		password := user.Password
		if password == "" {
			this.Data["json"] = ReturnError(4003, "密码不能为空")
		}
		status := models.IsUserMobile(mobile)
		if status {
			this.Data["json"] = ReturnError(4005, "此手机号已经注册")
			this.ServeJSON()
		} else {
			err = models.UserSave(mobile, password)
			if err == nil {
				this.Data["json"] = ReturnSuccess(0, "注册成功", nil, 0)
			} else {
				this.Data["json"] = ReturnError(5000, err)
			}
		}
	}
	this.Data["json"] = ReturnSuccess(200, "成功","1",1)
	this.ServeJSON()
}